package com.senlan.water.core.authority;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.collection.CollectionUtil;
import com.senlan.water.dao.domain.Role;
import com.senlan.water.dao.service.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义权限验证接口扩展
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private UserService userService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginKey) {
        return userService.getPermissionList(Long.parseLong(loginId.toString()));
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginKey) {
        List<Role> roleList = userService.getRoleList(Long.parseLong(loginId.toString()));
        if (CollectionUtil.isNotEmpty(roleList)){
            return roleList.stream().map(Role::getCode).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
